package com.mt.roll.opcua.s7.controller;


import com.mt.roll.opcua.opcua.controller.R;
import com.mt.roll.opcua.s7.my.s7connector.enmuc.PlcVarActual;
import com.mt.roll.opcua.s7.my.s7connector.enmuc.S7Client;
import com.mt.roll.opcua.s7.my.s7connector.service.S7Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/s7")
public class S7DemoController {
    private static final Logger logger = LoggerFactory.getLogger(S7DemoController.class);

    @Autowired
    S7Service s7Service;

    //demo1
    @PostMapping("/testReadAll")
    public R testReadAll() throws UnsupportedEncodingException, ParseException {
        for(PlcVarActual actual:PlcVarActual.values()){
            System.out.println(s7Service.read(actual,S7Client.S7_1500));;
        }
        return R.ok();
    }

    //demo2
    @PostMapping("/readTest")
    public R getTestForS7() throws UnsupportedEncodingException, ParseException {
        Boolean heartBeat = (Boolean)s7Service.read(PlcVarActual.HeartBeat,S7Client.S7_1500);
        String ddtl = (String)s7Service.read(PlcVarActual.DTL,S7Client.S7_1500);
        List<Character> characters = (List<Character>)s7Service.read(PlcVarActual.CharArrays,S7Client.S7_1500);

        List<Boolean> booleans = (List<Boolean>)s7Service.read(PlcVarActual.BooleanArrays,S7Client.S7_1500);
        String stri = (String)s7Service.read(PlcVarActual.STRING1,S7Client.S7_1500);

        return R.ok().put("res",heartBeat).put("characters",characters).put("ddtl",ddtl).put("bools",booleans).put("str",stri);
    }

    @PostMapping("/readTest2")
    public R getTest2ForS7() throws UnsupportedEncodingException, ParseException {
//        List<Character> characters = (List<Character>)s7Service.read(PlcVarActual.CharArrays,S7Client.S7_1500);
//
//        List<Boolean> booleans = (List<Boolean>)s7Service.read(PlcVarActual.BooleanArrays,S7Client.S7_1500);
//        String stri = (String)s7Service.read(PlcVarActual.STRING1,S7Client.S7_1500);
//
//        return R.ok().put("res",heartBeat).put("characters",characters).put("ddtl",ddtl).put("bools",booleans).put("str",stri);
        String con = "1233ADSDA";
        s7Service.write(PlcVarActual.STRING1, con, S7Client.S7_1500);
        String read = (String)s7Service.read(PlcVarActual.STRING1, S7Client.S7_1500);
        String sub0 = (String)s7Service.read(PlcVarActual.SUBID0, S7Client.S7_1500);


        return R.ok().put("str",read).put("sub0",sub0);
    }

    @PostMapping("/testForString")
    public R testForStrings() throws UnsupportedEncodingException, ParseException {
        //测试结果 l => 66ms
        long l = System.currentTimeMillis();
        String string = (String)s7Service.read(PlcVarActual.STRING1, S7Client.S7_1500);
        String[] subs = (String[])s7Service.read(PlcVarActual.SubIdArrays, S7Client.S7_1500);
        long l1 = System.currentTimeMillis();

        String[] toWrite = new String[60];
        for(int i=0;i<60;i++){
            int i1 = new Random().nextInt(100);
            toWrite[i] = "3011001210530111"+ i1;
        }
        ////测试结果 c => 57ms
        long c1 = System.currentTimeMillis();
        s7Service.write(PlcVarActual.SubIdArrays,toWrite,S7Client.S7_1500);
        long c2 = System.currentTimeMillis();
        String s = "cai xiang";
        s7Service.write(PlcVarActual.STRING1,s,S7Client.S7_1500);

        String string2 = (String)s7Service.read(PlcVarActual.STRING1, S7Client.S7_1500);
        String[] subs2 = (String[])s7Service.read(PlcVarActual.SubIdArrays, S7Client.S7_1500);

        return R.ok().put("l",(l1-l)).put("c",(c2-c1));
    }

    //demo3
    @PostMapping("/writeTest")
    public R writeTest() {
        s7Service.write(PlcVarActual.HeartBeat, false, S7Client.S7_1500);

        char[] charArrays_content = new char[2];
        charArrays_content[0] = '1';
        charArrays_content[1] = 'c';
        s7Service.write(PlcVarActual.CharArrays, charArrays_content, S7Client.S7_1500);

        boolean[] boolArrays_content = new boolean[2];
        boolArrays_content[0] = true;
        boolArrays_content[1] = false;
        s7Service.write(PlcVarActual.BooleanArrays, boolArrays_content, S7Client.S7_1500);

        String str = "你好啊aa";
        //todo string 的读写有问题 待会看看
        s7Service.write(PlcVarActual.STRING1, str, S7Client.S7_1500);
        return R.ok().put("res",true);
    }

}
